package com.siwei.mes.websocket.controller;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenmaolin
 * @version 1.0
 * @date 2023/4/13
 * @Description websocket日志接口
 */
@Controller
public class LogController {
    @Value("${spring.profiles.active}")
    private String active;

    @AnonymousAccess
    @RequestMapping("/logs")
    public String logs(Model model, HttpServletRequest request) {
        String socketUrl = this.getSocketUrl(request);
        String path = "/websocket/logging";
        socketUrl = this.getSocketUrl(socketUrl);
        model.addAttribute("socketUrl", socketUrl + path);
        model.addAttribute("targetName", "MES-品控实时日志");
        return "/logging";
    }

    @AnonymousAccess
    @RequestMapping("/jobLogs")
    public String jobLogs(Model model, HttpServletRequest request) {
        String socketUrl = this.getSocketUrl(request);
        String path = "/websocket/jobLogging";
        socketUrl = this.getSocketUrl(socketUrl);
        model.addAttribute("socketUrl", socketUrl + path);
        model.addAttribute("targetName", "MES-品控job实时日志");
        return "/logging";
    }

    @AnonymousAccess
    @RequestMapping("/erpLogs")
    public String erpLogs(Model model, HttpServletRequest request) {
        String socketUrl = this.getSocketUrl(request);
        String path = "/websocket/erpLogging";
        socketUrl = this.getSocketUrl(socketUrl);
        model.addAttribute("socketUrl", socketUrl + path);
        model.addAttribute("targetName", "MES-品控ERP实时日志");
        return "/logging";
    }

    @AnonymousAccess
    @RequestMapping("/tcLogs")
    public String tcLogs(Model model, HttpServletRequest request) {
        String socketUrl = this.getSocketUrl(request);
        String path = "/websocket/tcLogging";
        socketUrl = this.getSocketUrl(socketUrl);
        model.addAttribute("socketUrl", socketUrl + path);
        model.addAttribute("targetName", "MES-品控TC实时日志");
        return "/logging";
    }

    /**
     * 获取socket地址
     *
     * @param socketUrl
     * @return
     */
    private String getSocketUrl(String socketUrl) {
        switch (active) {
            case BaseParams.TEST:
                socketUrl = "ws://106.54.139.68:8094/mes-platform";
                break;
            case BaseParams.PROD:
                socketUrl = "ws://119.45.138.22:8094/mes-platform";
                break;
            default:
        }
        return socketUrl;
    }

    /**
     * 获取websocket地址
     * ws://106.54.139.68:8091/mes-platform/websocket/logging
     *
     * @param request
     * @return
     */
    public String getSocketUrl(HttpServletRequest request) {
        /**当前链接使用的协议*/
        return "ws" + "://" + request.getServerName()// 服务器地址
//                + "://" + serverName//服务器地址
                + ":" + request.getServerPort() // 端口号
                + request.getContextPath();
    }
}
